from apps.goods.models import SKU
from meiduo_mall.settings import BASE_DIR
from utils.goods import get_categories, get_breadcrumb, get_goods_specs
from apps.contents.models import ContentCategory
import time
import os


def generate_static_index_html():
    print("执行保存首页的任务----", time.ctime())
    # 1.商品分类数据
    categories = get_categories()

    # 2.广告数据
    contents = {}
    contents_categories = ContentCategory.objects.all()
    for cat in contents_categories:
        contents[cat.key] = cat.content_set.filter(status=True).order_by('sequence')

    # 我们的首页 页面静态化 定时任务
    # 我们把数据传递给模板
    context = {
        'categories': categories,
        'content': contents,
    }

    from django.template import loader
    # 1 加载渲染页面，将数据传给指定文件
    index_template = loader.get_template('index.html')
    index_html_data = index_template.render(context)
    from meiduo_mall.settings import BASE_DIR
    # 2..将数据传给模板
    file_path = os.path.join(os.path.dirname(BASE_DIR), "front_end_pc/index.html")
    # 3.保存渲染后的页面
    with open(file_path, 'w', encoding='utf-8') as f:
        f.write(index_html_data)




# 添加定时任务到系统中
# python manage.py crontab add
# 显示已经激活的定时任务
# python manage.py crontab show
# 移除定时任务
# python manage.py crontab remove
